import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test {
    private static List<Teacher> teachers = new ArrayList<>();
    private static List<Student> students = new ArrayList<>();
    private static List<Course> courses = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        boolean isRunning = true;
        while (isRunning) {
            printMenu();
            int choice = getIntInput("请输入操作序号：");
            switch (choice) {
                case 1:
                    addTeacher();
                    break;
                case 2:
                    addStudent();
                    break;
                case 3:
                    addCourse();
                    break;
                case 4:
                    teacherOpenCourse();
                    break;
                case 5:
                    studentSelectCourse();
                    break;
                case 6:
                    studentDropCourse();
                    break;
                case 7:
                    printStudentSchedule();
                    break;
                case 8:
                    System.out.println("系统退出，感谢使用！");
                    isRunning = false;
                    break;
                default:
                    System.out.println("无效操作序号，请重新输入！");
            }
        }
        scanner.close();
    }

    // 打印菜单
    private static void printMenu() {
        System.out.println("\n===== 学生选课模拟系统 =====");
        System.out.println("1. 添加教师");
        System.out.println("2. 添加学生");
        System.out.println("3. 添加课程");
        System.out.println("4. 教师开课");
        System.out.println("5. 学生选课");
        System.out.println("6. 学生退课");
        System.out.println("7. 打印学生课表");
        System.out.println("8. 退出系统");
        System.out.println("=========================");
    }

    // 获取整数输入并校验
    private static int getIntInput(String prompt) {
        System.out.print(prompt);
        while (!scanner.hasNextInt()) {
            System.out.println("请输入有效的整数！");
            System.out.print(prompt);
            scanner.next();
        }
        int input = scanner.nextInt();
        scanner.nextLine();
        return input;
    }

    // 获取字符串输入
    private static String getStringInput(String prompt) {
        System.out.print(prompt);
        return scanner.nextLine();
    }

    // 添加教师
    private static void addTeacher() {
        String id = getStringInput("请输入教师编号：");
        String name = getStringInput("请输入教师姓名：");
        String gender = getStringInput("请输入教师性别：");
        Teacher teacher = new Teacher(id, name, gender);
        teachers.add(teacher);
        System.out.println("教师添加成功！");
    }

    // 添加学生（包含年级信息）
    private static void addStudent() {
        String id = getStringInput("请输入学生编号：");
        String name = getStringInput("请输入学生姓名：");
        String gender = getStringInput("请输入学生性别：");
        String grade = getStringInput("请输入学生年级：");
        Student student = new Student(id, name, gender, grade);
        students.add(student);
        System.out.println("学生添加成功！");
    }

    // 添加课程
    private static void addCourse() {
        String id = getStringInput("请输入课程编号：");
        String name = getStringInput("请输入课程名称：");
        String location = getStringInput("请输入上课地点：");
        String time = getStringInput("请输入上课时间：");
        Course course = new Course(id, name, location, time);
        courses.add(course);
        System.out.println("课程添加成功！");
    }

    // 教师开课
    private static void teacherOpenCourse() {
        if (teachers.isEmpty()) {
            System.out.println("暂无教师，请先添加教师！");
            return;
        }
        if (courses.isEmpty()) {
            System.out.println("暂无课程，请先添加课程！");
            return;
        }
        System.out.println("===== 教师列表 =====");
        for (int i = 0; i < teachers.size(); i++) {
            Teacher teacher = teachers.get(i);
            System.out.println(i + 1 + ". " + teacher.getId() + "-" + teacher.getName());
        }
        int teacherIdx = getIntInput("请选择教师序号：");
        if (teacherIdx < 1 || teacherIdx > teachers.size()) {
            System.out.println("无效序号！");
            return;
        }
        Teacher teacher = teachers.get(teacherIdx - 1);

        System.out.println("===== 课程列表 =====");
        for (int i = 0; i < courses.size(); i++) {
            Course course = courses.get(i);
            System.out.println(i + 1 + ". " + course.getId() + "-" + course.getName());
        }
        int courseIdx = getIntInput("请选择课程序号：");
        if (courseIdx < 1 || courseIdx > courses.size()) {
            System.out.println("无效序号！");
            return;
        }
        Course course = courses.get(courseIdx - 1);

        teacher.setTaughtCourse(course);
        course.setTeacher(teacher);
        System.out.println(teacher.getName() + " 成功开设 " + course.getName() + " 课程！");
    }

    // 学生选课
    private static void studentSelectCourse() {
        if (students.isEmpty()) {
            System.out.println("暂无学生，请先添加学生！");
            return;
        }
        List<Course> availableCourses = new ArrayList<>();
        for (Course course : courses) {
            if (course.getTeacher() != null) {
                availableCourses.add(course);
            }
        }
        if (availableCourses.isEmpty()) {
            System.out.println("暂无可选课程，请先让教师开课！");
            return;
        }
        System.out.println("===== 学生列表 =====");
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            System.out.println(i + 1 + ". " + student.getId() + "-" + student.getName() + "(" + student.getGrade() + "年级)");
        }
        int studentIdx = getIntInput("请选择学生序号：");
        if (studentIdx < 1 || studentIdx > students.size()) {
            System.out.println("无效序号！");
            return;
        }
        Student student = students.get(studentIdx - 1);

        System.out.println("===== 可选课程列表 =====");
        for (int i = 0; i < availableCourses.size(); i++) {
            Course course = availableCourses.get(i);
            System.out.println(i + 1 + ". " + course.getId() + "-" + course.getName() + "（教师：" + course.getTeacher().getName() + "）");
        }
        int courseIdx = getIntInput("请选择课程序号：");
        if (courseIdx < 1 || courseIdx > availableCourses.size()) {
            System.out.println("无效序号！");
            return;
        }
        Course course = availableCourses.get(courseIdx - 1);

        student.selectCourse(course);
        System.out.println(student.getName() + " 成功选择 " + course.getName() + " 课程！");
    }

    // 学生退课
    private static void studentDropCourse() {
        if (students.isEmpty()) {
            System.out.println("暂无学生，请先添加学生！");
            return;
        }
        System.out.println("===== 学生列表 =====");
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            System.out.println(i + 1 + ". " + student.getId() + "-" + student.getName() + "(" + student.getGrade() + "年级)");
        }
        int studentIdx = getIntInput("请选择学生序号：");
        if (studentIdx < 1 || studentIdx > students.size()) {
            System.out.println("无效序号！");
            return;
        }
        Student student = students.get(studentIdx - 1);

        Course selectedCourse = student.getSelectedCourse();
        if (selectedCourse == null) {
            System.out.println(student.getName() + " 暂无选课记录！");
            return;
        }

        student.dropCourse();
        System.out.println(student.getName() + " 成功退选 " + selectedCourse.getName() + " 课程！");
    }

    // 打印学生课表
    private static void printStudentSchedule() {
        if (students.isEmpty()) {
            System.out.println("暂无学生，请先添加学生！");
            return;
        }
        System.out.println("===== 学生列表 =====");
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            System.out.println(i + 1 + ". " + student.getId() + "-" + student.getName() + "(" + student.getGrade() + "年级)");
        }
        int studentIdx = getIntInput("请选择学生序号：");
        if (studentIdx < 1 || studentIdx > students.size()) {
            System.out.println("无效序号！");
            return;
        }
        Student student = students.get(studentIdx - 1);

        System.out.println("\n学生 " + student.getName() + "（" + student.getGrade() + "年级）的课表：");
        Course course = student.getSelectedCourse();
        if (course != null) {
            System.out.println("课程编号：" + course.getId());
            System.out.println("课程名称：" + course.getName());
            System.out.println("上课地点：" + course.getLocation());
            System.out.println("上课时间：" + course.getTime());
            System.out.println("授课教师：" + course.getTeacher().getName());
        } else {
            System.out.println("暂未选课");
        }
        System.out.println("------------------------");
    }
}
